package com.dsp.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.dsp.bean.Message;
import com.dsp.bean.SelectTag;
import com.dsp.services.SelectTagService;

@Controller("SelectTagsController")
public class SelectTagsController extends DspController<SelectTag>{
	
	public static String[] QueryColumn = {"tagId","tagName"};
	public static String[] QueryType = {"String","String"};
	
	public SelectTagsController() {
		super(SelectTag.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	@Autowired
	private SelectTagService selectTagService;

	@RequestMapping(value = "/tags", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/tags/list");
		mav.addObject("current", "tags");
		return mav;
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/tags_list")
	@ResponseBody
	public Pagination<SelectTag> index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(SelectTag.class);
		super.getPageParameters(dc, QueryColumn, QueryType);
		Pagination<SelectTag> p = this.selectTagService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	/**
	 * 
	 * @param tags
	 * @return
	 */
	@RequestMapping(value = "/tags_create", method = RequestMethod.POST)
	@ResponseBody
	public Message create(@RequestBody SelectTag tags) {
		Message message = new Message();
		logger.info("正在创建标签......");
		try {
			if (tags.getId() != null) {
				this.selectTagService.update(tags);
			} else {
				this.selectTagService.save(tags);
			}
		} catch (Exception se) {
			se.getStackTrace();
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(se.getLocalizedMessage());
			message.setMsg(msg);
		}
		logger.info("完成创建......");
		return message;
	}

	@RequestMapping(value = "/tags_edit", method = RequestMethod.POST)
	@ResponseBody
	public SelectTag edit(@RequestBody SelectTag _tags) {
		SelectTag tags = this.selectTagService.findById(_tags.getId());
		return tags;
	}

	@RequestMapping(value = "/tags_delete", method = RequestMethod.POST)
	@ResponseBody
	public Message delete(@RequestBody SelectTag tags) {
		Message message = new Message();
		String succ = this.selectTagService.delete(tags);
		message.setSuccess(Boolean.valueOf(succ));
		return message;
	}

	/**
	 * @return the selectTagService
	 */
	public SelectTagService getSelectTagService() {
		return selectTagService;
	}

	/**
	 * @param selectTagService
	 *            the selectTagService to set
	 */
	public void setSelectTagService(SelectTagService selectTagService) {
		this.selectTagService = selectTagService;
	}

}
